package com.i2bt.app.share.model;

import com.i2bt.common.AndroidRestContract;
import com.i2bt.common.UserConfig;

import com.i2bt.app.BaseFragment;
import com.i2bt.app.BaseJsonModel;
import com.i2bt.app.share.ShareFragment;
import com.i2bt.bean.GetSharedBean;
import com.i2bt.util.FileSizeConvertUtil;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

public class GetShareJsonHandle extends BaseJsonModel {
	ShareFragment frg;

	public GetShareJsonHandle(BaseFragment fragment) {
		super(fragment);
		// TODO Auto-generated constructor stub
		this.frg = (ShareFragment) fragment;
	}

	@Override
	protected String logicToWrap() throws JSONException {
		// TODO Auto-generated method stub
		try {
			jsonParam.put(AndroidRestContract.ACTION,
					AndroidRestContract.GET_FRIEND_SHARE);
			jsonParam.put(AndroidRestContract.TOKEN, UserConfig.token);
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return jsonParam.toString();
	}

	@Override
	protected void logicToUnwrap(boolean isSuc, String result)
			throws JSONException {
		// TODO Auto-generated method stub
		if (isSuc) {
			try {
				JSONObject ja = new JSONObject(result);
				boolean succ = ja.getBoolean(AndroidRestContract.SUCCEED);
				String mes = null;
				if (succ) {
					if (ja.has(AndroidRestContract.SHARE_INFO)) {
						JSONArray list = ja
								.getJSONArray(AndroidRestContract.SHARE_INFO);
						this.frg.friendSharedFilesList.clear();
						for (int i = 0; i < list.length(); i++) {
							JSONObject item = list.getJSONObject(i);
							GetSharedBean bean = new GetSharedBean();

							bean.setFriendName(item
									.getString(AndroidRestContract.SHARE_FRIEND_NAME));

							bean.setShareDate(item
									.getString(AndroidRestContract.SHARE_DATE));

							bean.setFileSize(item
									.getString(AndroidRestContract.FILE_SIZE));
							bean.setHomeBoxPath(item
									.getString(AndroidRestContract.SHARE_FOLDERS));

							bean.setFileName(item
									.getString(AndroidRestContract.SHARE_HOMEBOX_NAME));

							bean.setFile(this.isFile(bean.getFileSize()));
							bean.setHomebox(this.isHomeBox(
									bean.getHomeBoxPath(), bean.isFile()));
							if (!bean.isHomebox()) {
								bean.setFileName(this.getFileName(bean
										.getHomeBoxPath()));
							}
							bean.setFileSize(FileSizeConvertUtil
									.autoConvert(Integer.parseInt(bean
											.getFileSize())));
							this.frg.friendSharedFilesList.add(bean);
						}
					} else {
						this.frg.toastHandler("没有收到分享");
					}
					this.frg.friendSharedListAdapter.notifyDataSetChanged();
					this.frg.friendSharedListView.stopRefresh();
				} else {
					this.frg.toastHandler(ja.get(AndroidRestContract.MESSAGE)
							.toString());
					this.frg.friendSharedListView.stopRefresh();
				}
			} catch (JSONException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				this.frg.friendSharedListView.stopRefresh();
			}
		} else {
			this.frg.toastHandler("网络连接失败");
			this.frg.friendSharedListView.stopRefresh();
		}
	}

	public boolean isFile(String size) {
		if (size.equals("0")) {
			return false;
		} else {
			return true;
		}
	}

	public boolean isHomeBox(String path, boolean isFile) {
		if (isFile) {
			return false;
		}
		if (path.indexOf('\\') == -1) {
			return true;
		}
		return false;
	}

	public String getFileName(String path) {
		String str = path;
		int k = 0;
		for (int i = 0; i < str.length(); i++) {
			k = str.indexOf('\\');
			if (k == -1) {
				break;
			}
			str = str.substring(k + 1);
		}
		return str;
	}
}
